using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class cTrapOli : MonoBehaviour {

	class cCarInfo 
	{
		public cCar _car;
		public float _inTime;
		public float _rol;
	}
	
	List<cCarInfo> incomeList = new List<cCarInfo>();

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		Queue<cCarInfo> removeObj = new Queue<cCarInfo>();
		
		foreach ( cCarInfo ci in incomeList )
		{
			
			ci._inTime += Time.deltaTime;
			
			ci._car.transform.Rotate(Vector3.up * ci._rol * Time.deltaTime);

			if ( ci._inTime > 2f || ci._car.bCrash )
			{
				removeObj.Enqueue( ci );

			}

		}
		
		foreach( cCarInfo ci in removeObj )
		{
			incomeList.Remove( ci );
		}
		
	}
	
	void OnTriggerEnter(Collider obj) {
		
		if ( obj.name == "Car" )
		{
			
			cCar inCar = (cCar)obj.GetComponent("cCar");
			
			if ( inCar )
			{
				bool bFound = false;
				foreach ( cCarInfo ci in incomeList )
				{
					if ( ci._car == inCar )
						bFound = true;
				}
				if ( !bFound )
				{
					cCarInfo addcar = new cCarInfo();
					addcar._car = inCar;
					addcar._inTime = 0f;
					addcar._rol = Random.Range(100f,300f);
					if ( Random.Range(0,10) > 5 ) addcar._rol *= -1;
					incomeList.Add( addcar );
				}
			}

		}
	}
}
